package com.scsystem.xysc.controller;


import com.scsystem.xysc.pojo.vo.ReturnResult;
import com.scsystem.xysc.service.StoreDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/storeData")
@CrossOrigin
@Tag(name = "店铺数据")
@Slf4j
public class StoreDataController {

    @Resource
    private StoreDataService storeDataService;

    @GetMapping("/getStoreDataForIndex")
    @Operation(summary = "获取该店铺本月内的成单率")
    @Parameter(name = "sid", description = "店铺id")
    public ReturnResult<Map<String, Double>> getStoreDataForIndex(Long sid) {
        Map<String, Double> map = new HashMap<>();
        map.put("今日", storeDataService.calculateTodayVisitCompletionRate(sid));  //今日
        map.put("昨日", storeDataService.calculateYesterdayVisitCompletionRate(sid));  //昨日
        map.put("本周", storeDataService.calculateWeeklyVisitCompletionRate(sid));  //本周
        map.put("本月", storeDataService.calculateMonthlyVisitCompletionRate(sid));  //本月
        return new ReturnResult<>(200, "成功", map);
    }

    @PostMapping("/record-customer-visit")
    @Operation(summary = "新增店铺数据")
    @Parameter(name = "storeId", description = "店铺id")
    @Parameter(name = "isDeal", description = "是否成交（true or false）")
    public ResponseEntity<String> recordCustomerVisit(
            HttpServletRequest request,
            @RequestParam("storeId") Long storeId,
            @RequestParam("isDeal") boolean isDeal) {
        try {
            storeDataService.recordCustomerVisit(request.getRemoteAddr(), storeId, isDeal);
            return ResponseEntity.ok("访问信息已记录");
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("记录访问信息时出错");
        }
    }

}
